﻿using System;
using System.Text;
using System.Threading;
using UDS.Components;
using System.Data;

namespace UDS.Entity
{
    public class CFFileInfo
    {
        #region properties
        public int FileID
        {
            get;
            set;
        }

        public int CatalogID
        {
            get;
            set;
        }

        public string Catalog
        {
            get;
            private set;
        }

        public string Name
        {
            get;
            set;
        }

        public string Version
        {
            get;
            set;
        }

        public int Size
        {
            get;
            set;
        }

        public string VPath
        {
            get;
            set;
        }

        public string DownloadablePath
        {
            get
            {
                if (string.IsNullOrEmpty(VPath))
                {
                    return "javascript:void(0);";
                }
                return HttpUtil.ResolveUrl(VPath);
            }
        }

        /// <summary>
        /// The username of the creator.
        /// </summary>
        public string Creator
        {
            get;
            set;
        }

        private string creatorRealName;

        /// <summary>
        /// Get the real name of the creator.
        /// </summary>
        public string CreatorRealName
        {
            get
            {
                if (creatorRealName == null)
                {
                    Interlocked.CompareExchange(ref creatorRealName, Staff.GetRealNameByUsername(Creator), null);
                }
                return creatorRealName;
            }
        }

        public bool Invalid
        {
            get;
            set;
        }

        public DateTime TimeCreated
        {
            get;
            set;
        }

        public DateTime LastModified
        {
            get;
            set;
        }
        #endregion

        internal static CFFileInfo ReadFileInfo(IDataReader dr)
        {
            CFFileInfo f = new CFFileInfo();
            f.FileID = (int)dr["FileID"];
            f.CatalogID = (int)dr["CatalogID"];
            f.Catalog = Convert.ToString(dr["Catalog"]);
            f.Name = Convert.ToString(dr["Name"]);
            f.Version = Convert.ToString(dr["Version"]);
            f.Size = SQLUtil.ReadInt32(dr["Size"], 0);
            f.VPath = Convert.ToString(dr["VPath"]);
            f.Creator = Convert.ToString(dr["Creator"]);
            f.Invalid = Convert.ToBoolean(dr["Invalid"]);
            f.TimeCreated = Convert.ToDateTime(dr["TimeCreated"]);
            f.LastModified = Convert.ToDateTime(dr["LastModified"]);
            return f;
        }
    }
}
